import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-03-15
 * Time:22:36
 */
public class TestjdbcDelete {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();//服务器
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //连接
        Connection connection = dataSource.getConnection();
        //sql语句
        String sql = "delete from student where name = ?";
        Scanner input = new Scanner(System.in);
        System.out.println("请输入你要删除的名字:");
        String name = input.next();
        //转换成对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        System.out.println("statement =" + statement);
        //执行
        int a = statement.executeUpdate();
        System.out.println(a);
        //回收资源
        statement.close();
        connection.close();
    }
}
